package com.quick.develop.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.quick.develop.framework.common.annotation.CreateForm;
import com.quick.develop.framework.common.annotation.DetailForm;
import com.quick.develop.framework.common.annotation.QueryTable;
import com.quick.develop.framework.common.annotation.UpdateForm;
import lombok.AllArgsConstructor;
import com.quick.develop.framework.common.utils.PageResult;
import com.quick.develop.framework.mybatis.service.impl.BaseServiceImpl;
import com.quick.develop.business.convert.EnvironmentalEmergencyConvert;
import com.quick.develop.business.entity.EnvironmentalEmergencyEntity;
import com.quick.develop.business.query.EnvironmentalEmergencyQuery;
import com.quick.develop.business.vo.EnvironmentalEmergencyVO;
import com.quick.develop.business.dao.EnvironmentalEmergencyDao;
import com.quick.develop.business.service.EnvironmentalEmergencyService;
import com.quick.develop.form.utils.ExcelUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 环境应急预案
 *
 * @author GJH 1906559468@qq.com
 * @since 1.0.0 2024-01-27
 */
@Service
@AllArgsConstructor
public class EnvironmentalEmergencyServiceImpl extends BaseServiceImpl<EnvironmentalEmergencyDao, EnvironmentalEmergencyEntity> implements EnvironmentalEmergencyService {

    @QueryTable
    @Override
    public PageResult<EnvironmentalEmergencyVO> page(EnvironmentalEmergencyQuery query) {
        IPage<EnvironmentalEmergencyEntity> page = baseMapper.selectPage(getPage(query), getWrapperOptional(query));

        return new PageResult<>(EnvironmentalEmergencyConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<EnvironmentalEmergencyEntity> getWrapper(EnvironmentalEmergencyQuery query) {
        LambdaQueryWrapper<EnvironmentalEmergencyEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    private LambdaQueryWrapper<EnvironmentalEmergencyEntity> getWrapperOptional(EnvironmentalEmergencyQuery query) {
        LambdaQueryWrapper<EnvironmentalEmergencyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!StrUtil.isBlankIfStr(query.getId()), EnvironmentalEmergencyEntity::getId, query.getId());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getBusinessKey()), EnvironmentalEmergencyEntity::getBusinessKey, query.getBusinessKey());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getAttachId()), EnvironmentalEmergencyEntity::getAttachId, query.getAttachId());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCategory()), EnvironmentalEmergencyEntity::getCategory, query.getCategory());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getName()), EnvironmentalEmergencyEntity::getName, query.getName());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getFrequency()), EnvironmentalEmergencyEntity::getFrequency, query.getFrequency());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getAssignTaskStatus()), EnvironmentalEmergencyEntity::getAssignTaskStatus, query.getAssignTaskStatus());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getExecutionRequest()), EnvironmentalEmergencyEntity::getExecutionRequest, query.getExecutionRequest());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentName()), EnvironmentalEmergencyEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentName());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalName()), EnvironmentalEmergencyEntity::getResponsiblePersonalName, query.getResponsiblePersonalName());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCode()), EnvironmentalEmergencyEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCode());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCompletionDeadline()), EnvironmentalEmergencyEntity::getCompletionDeadline, query.getCompletionDeadline());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCompletionDate()), EnvironmentalEmergencyEntity::getCompletionDate, query.getCompletionDate());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getProcessStatus()), EnvironmentalEmergencyEntity::getProcessStatus, query.getProcessStatus());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCloseDate()), EnvironmentalEmergencyEntity::getCloseDate, query.getCloseDate());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserCode()), EnvironmentalEmergencyEntity::getCreatedUserCode, query.getCreatedUserCode());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedUserName()), EnvironmentalEmergencyEntity::getCreatedUserName, query.getCreatedUserName());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getCreatedTime()), EnvironmentalEmergencyEntity::getCreatedTime, query.getCreatedTime());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserCode()), EnvironmentalEmergencyEntity::getUpdatedUserCode, query.getUpdatedUserCode());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedUserName()), EnvironmentalEmergencyEntity::getUpdatedUserName, query.getUpdatedUserName());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getUpdatedTime()), EnvironmentalEmergencyEntity::getUpdatedTime, query.getUpdatedTime());
        wrapper.eq(!StrUtil.isBlankIfStr(query.getDelFlag()), EnvironmentalEmergencyEntity::getDelFlag, query.getDelFlag());
        wrapper.like(!StrUtil.isBlankIfStr(query.getIdLike()), EnvironmentalEmergencyEntity::getId, query.getIdLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getBusinessKeyLike()), EnvironmentalEmergencyEntity::getBusinessKey, query.getBusinessKeyLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getAttachIdLike()), EnvironmentalEmergencyEntity::getAttachId, query.getAttachIdLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCategoryLike()), EnvironmentalEmergencyEntity::getCategory, query.getCategoryLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getNameLike()), EnvironmentalEmergencyEntity::getName, query.getNameLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getFrequencyLike()), EnvironmentalEmergencyEntity::getFrequency, query.getFrequencyLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getAssignTaskStatusLike()), EnvironmentalEmergencyEntity::getAssignTaskStatus, query.getAssignTaskStatusLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getExecutionRequestLike()), EnvironmentalEmergencyEntity::getExecutionRequest, query.getExecutionRequestLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getResponsibleDepartmentNameLike()), EnvironmentalEmergencyEntity::getResponsibleDepartmentName, query.getResponsibleDepartmentNameLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalNameLike()), EnvironmentalEmergencyEntity::getResponsiblePersonalName, query.getResponsiblePersonalNameLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getResponsiblePersonalCodeLike()), EnvironmentalEmergencyEntity::getResponsiblePersonalCode, query.getResponsiblePersonalCodeLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCompletionDeadlineLike()), EnvironmentalEmergencyEntity::getCompletionDeadline, query.getCompletionDeadlineLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCompletionDateLike()), EnvironmentalEmergencyEntity::getCompletionDate, query.getCompletionDateLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getProcessStatusLike()), EnvironmentalEmergencyEntity::getProcessStatus, query.getProcessStatusLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCloseDateLike()), EnvironmentalEmergencyEntity::getCloseDate, query.getCloseDateLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserCodeLike()), EnvironmentalEmergencyEntity::getCreatedUserCode, query.getCreatedUserCodeLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedUserNameLike()), EnvironmentalEmergencyEntity::getCreatedUserName, query.getCreatedUserNameLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getCreatedTimeLike()), EnvironmentalEmergencyEntity::getCreatedTime, query.getCreatedTimeLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserCodeLike()), EnvironmentalEmergencyEntity::getUpdatedUserCode, query.getUpdatedUserCodeLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedUserNameLike()), EnvironmentalEmergencyEntity::getUpdatedUserName, query.getUpdatedUserNameLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getUpdatedTimeLike()), EnvironmentalEmergencyEntity::getUpdatedTime, query.getUpdatedTimeLike()).or();
        wrapper.like(!StrUtil.isBlankIfStr(query.getDelFlagLike()), EnvironmentalEmergencyEntity::getDelFlag, query.getDelFlagLike()).or();
        return wrapper;
    }

    @CreateForm
    @Override
    public void save(EnvironmentalEmergencyVO vo) {
        EnvironmentalEmergencyEntity entity = EnvironmentalEmergencyConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @UpdateForm
    @Override
    public void update(EnvironmentalEmergencyVO vo) {
        EnvironmentalEmergencyEntity entity = EnvironmentalEmergencyConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<String> idList) {
        removeByIds(idList);
    }

    @Override
    public EnvironmentalEmergencyVO getEnvironmentalEmergencyVO(String id) {
        EnvironmentalEmergencyEntity entity = getById(id);
        return EnvironmentalEmergencyConvert.INSTANCE.convert(entity);
    }

    @DetailForm
    @Override
    public EnvironmentalEmergencyVO getEnvironmentalEmergencyByBusinessKey(String businessKey) {
        EnvironmentalEmergencyEntity entity = baseMapper.selectOne(new LambdaQueryWrapper<EnvironmentalEmergencyEntity>()
                .eq(EnvironmentalEmergencyEntity::getBusinessKey, businessKey));
        return EnvironmentalEmergencyConvert.INSTANCE.convert(entity);
    }

    @Override
    public List<EnvironmentalEmergencyVO> getListByKeys(EnvironmentalEmergencyQuery query) {
        return this.getListByKeys(query.getFieldName(), query.getCodes());
    }

    @Override
    public void dynamicExportExcel(EnvironmentalEmergencyQuery query) {
        List<EnvironmentalEmergencyEntity> list = this.list(getWrapperOptional(query));
        ExcelUtils.downloadExcel(EnvironmentalEmergencyEntity.class, list, "环境应急预案导出" );
    }

    @Override
    public void dynamicImportExcel(MultipartFile file) {
        ExcelUtils.execWriteDatabase(
                file,
                EnvironmentalEmergencyEntity.class,
                this::filterDynamicImport,
                this::overrideDynamicImport,
                this::updateBatchById,
                this::saveBatch
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchSave(List<EnvironmentalEmergencyVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalEmergencyEntity> entities = list.stream().map(EnvironmentalEmergencyConvert.INSTANCE::convert).collect(Collectors.toList());
            saveBatch(entities);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchUpdateById(List<EnvironmentalEmergencyVO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<EnvironmentalEmergencyEntity> entities = list.stream().map(EnvironmentalEmergencyConvert.INSTANCE::convert).collect(Collectors.toList());
            updateBatchById(entities);
        }
    }

    public List<EnvironmentalEmergencyVO> getListByKeys(String key, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys) && StrUtil.isNotEmpty(key)) {
            return Collections.emptyList();
        }
        return EnvironmentalEmergencyConvert.INSTANCE.convertList(list(Wrappers.query(EnvironmentalEmergencyEntity.class).in(key, keys)));
    }

    /**
     * @param sFunction 函数式获取Key
     * @param keys      需要查询的列表
     * @return 返回数据列表
     */
    public List<EnvironmentalEmergencyEntity> getListByKeys(SFunction<EnvironmentalEmergencyEntity, ?> sFunction, Collection<Serializable> keys) {
        if (CollUtil.isNotEmpty(keys)) {
            return Collections.emptyList();
        }
        return list(Wrappers.lambdaQuery(EnvironmentalEmergencyEntity.class).in(sFunction, keys));
    }

    /**
     * @param list 解析的数据
     * @return 过滤以后的数据
     */
    public List<EnvironmentalEmergencyEntity> filterDynamicImport(List<EnvironmentalEmergencyEntity> list) {
        return list;
    }

    /**
     * @param list 解析的数据
     * @return 返回过滤的数据
     * @description 覆盖导入的数据
     */
    public List<EnvironmentalEmergencyEntity> overrideDynamicImport(List<EnvironmentalEmergencyEntity> list) {
        return Collections.emptyList();
    }
}